@extends('admin.layouts.main')
@section('header')
    @parent
    <style>
        .quoteBox{
            padding: 5px;
            margin-top: 10px;
        }
    </style>
@endsection
@section('content')
    <blockquote class="layui-elem-quote quoteBox">

    </blockquote>
    <div style="height: 100%">
        <form class="layui-form">
            <table class="layui-hidden layui-form layui-table" id="treeTable" lay-filter="treeTable"></table>
            <div class="layui-form-item">
                <div class="layui-input-block">

                    <button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="submit">立即提交</button>
                </div>
            </div>
        </form>
    </div>
@endsection

@section('footer')
    @parent
    <script type="text/javascript">
        layui.config({
            base:'{{ asset('xman/extends') }}/'
        }).extend({
            treetable: 'treetable',
            eleTree:'eleTree/eleTree'
        }).use([ 'laytpl','treetable','table','form','eleTree'],function () {
            var laytpl = layui.laytpl,
                table = layui.table,
                form = layui.form,
                eleTree = layui.eleTree;

            // 渲染表格
            var checkedIds;
            var roleId = getUrlParam('id');
            var renderTable = function (){
                layer.load(2);
                $.get('{{ url('admin/role/menuList') }}',{id:roleId},function (res) {
                    if(res.status == 200){
                        var data = res.data;
                        var eleIndex = eleTree.render({
                            elem: "#treeTable",
                            data: data.menus,
                            checkStrictly:true,
                            defaultCheckedKeys:data.menuIds,
                            emptText: "暂无数据",
                            showCheckbox: true
                        });

                        eleTree.on('nodeChecked(treeTable)',function (d) {
                            var checkedData =  eleIndex.getChecked(false,true);
                            var ids = [];
                           if(checkedData){
                               $.each(checkedData,function (i,e) {
                                   ids.push(e.id);
                               })
                           }
                           checkedIds = ids;
                        });
                        layer.closeAll('loading');
                    }else {
                        console.log(res.msg);
                        layer.closeAll('loading');
                        return false;
                    }
                },'json');
            };
            renderTable();
            form.on('submit(submit)',function () {
               goConfirm('{{ url('admin/role/menu') }}',{id:roleId,menuIds:checkedIds},'确认保存？',0);
                return false;
            });
        })
    </script>
@endsection